package com.example.blog.service;

import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.mapper.UserInfoMapper;
import com.example.blog.model.UserInfo;
import com.example.blog.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private BlogInfoMapper blogInfoMapper;

    public UserInfo register(String userName, String password) {
        UserInfo user = new UserInfo();
        user.setUserName(userName);
        user.setPassword(SecurityUtils.encrypt(password));
        Integer code = userInfoMapper.insertUser(user);
        if(code != 1) {
            return null;
        }
        return user;
    }
    public UserInfo getUserByName(String userName) {
        return userInfoMapper.selectByUserName(userName);
    }

    public UserInfo getUserById(Integer id) {
        return userInfoMapper.selectById(id);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        //通过博客id获取用户id
        Integer userId = blogInfoMapper.selectUserIdByBlogId(blogId);
        //通过用户id获取用户信息
        return userInfoMapper.selectById(userId);
    }
}
